import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author krystal
 * @date 2022/12/1 15:30
 */
@WebServlet("/showRequest")
public class ShowRequestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // resp 是响应对象, setContentType, 给响应的 ContentType 设置了值. html
        // 声明响应 body 是 html 结构的数据.
        resp.setContentType("text/html");
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append(req.getProtocol());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getMethod());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getRequestURI());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getContextPath());
        stringBuilder.append("<br>");
        stringBuilder.append(req.getQueryString());
        stringBuilder.append("<br>");

        //把请求的header也拼进来
        Enumeration<String> headNames=req.getHeaderNames();
        while (headNames.hasMoreElements()){
            String name=headNames.nextElement();
            String value=req.getHeader(name);
            stringBuilder.append(name+":"+value);
            stringBuilder.append("<br>");
        }

        resp.getWriter().write(stringBuilder.toString());
        //System.out.println(stringBuilder.toString());
    }
}
